﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace TLWindowsEditorWinFormsDemo
{
    /// <summary>
    /// 华为API 身份认证配置
    /// by IT_MAOFY 2024/05/29
    /// 需要在web.config中配置如下：
    /// &lt;configSections&gt;
    ///	&lt; section name = "languageCodeSettings" type="TLWindowsEditorWinFormsDemo.LanguageCodeSettings, TLWindowsEditorWinFormsDemo"/&gt;
    /// &lt;/configSections&gt;
    /// 
    /// &lt;languageCodeSettings&gt;
    ///	&lt;items&gt;
    ///		&lt;item value="xxx" text="xxx" &gt;&lt;/item&gt;
    ///		&lt;item value="xxx" text="xxx" &gt;&lt;/item&gt;	
    ///	&lt;/items>
    /// &lt;/languageCodeSettings&gt;
    /// </summary>
    public class LanguageCodeSettings : ConfigurationSection
    {

        [ConfigurationProperty("items")]
        public LanguageCodeSettingsCollection Items
        {
            get { return (LanguageCodeSettingsCollection)this["items"]; }
            set { this["items"] = value; }
        }

        /// <summary>
        /// 默认语言
        /// </summary>
        [ConfigurationProperty("defaultValue", IsRequired = true)]
        public string DefaultValue
        {
            get { return (string)this["defaultValue"]; }
            set { this["defaultValue"] = value; }
        }

        public IList<LanguageCodeElement> GetList()
        {
            return GetListInternal().ToList();
        }

        private IEnumerable<LanguageCodeElement> GetListInternal()
        {
            foreach (LanguageCodeElement auth in Items)
            {
                yield return auth;
            }
            yield break;
        }
    }


    [ConfigurationCollection(typeof(LanguageCodeSettings), AddItemName = "item")]
    public class LanguageCodeSettingsCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new LanguageCodeElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((LanguageCodeElement)element).Value;
        }
    }


    public class LanguageCodeElement : ConfigurationElement
    {
        /// <summary>
        /// Value
        /// </summary>
        [ConfigurationProperty("value", IsRequired = true)]
        public string Value
        {
            get { return (string)this["value"]; }
            set { this["value"] = value; }
        }

        /// <summary>
        /// Text
        /// </summary>
        [ConfigurationProperty("text", IsRequired = true)]
        public string Text
        {
            get { return (string)this["text"]; }
            set { this["text"] = value; }
        }
    }
}
